define(function (require, exports, module)
{
  require('jx_check_css');
  var $ = require('jquery');
  var jxKit = require('jx_kit');
  var msgbox = require('jx_msgbox');
  var jxbaseinfo = require('jxbaseinfo');
  var $jxCheck =
    {
      checkMethods: [],
      checkInfos: [],
      errorIds: [],
      msgTitle:null,
      check: function ()
      {
        this.checkInfos.length = 0;
        this.errorIds.length = 0;
        var checkFlag = this.simpleCheck();
        for (key in this.checkMethods)
        {
          if (checkFlag)
            checkFlag = this.checkMethods[key].call();
          else
            this.checkMethods[key].call();
        }
        if (!checkFlag)
          this.showDialog();
        return checkFlag;
      },
      addCheckInfo: function (checkInfo)
      {
        this.checkInfos.push(checkInfo);
      },
      addErrorId: function (errorId)
      {
        this.errorIds.push(errorId);
      },
      addCheckMethod: function (m)
      {
        this.checkMethods.push(m);
      },
      mCheck: function (callback)
      {
        this.checkInfos.length = 0;
        this.errorIds.length = 0;
        return callback.call();
      },
      inputFocus: function (obj)
      {
        this.showTip(obj);
      },
      inputBlur: function (obj)
      {
        this.hideTip();
        if (!this.inputCheck(obj))
          this.showError(obj, this.checkInfo);
        else
          this.hideError();
      },
      hideTip: function ()
      {
        $(".txt-tips").fadeOut("fast");
      },
      showTip: function (obj)
      {
        if (undefined == obj.attr("jtip"))
          return;

        $("#jx-txt-tips").html(obj.attr("jtip"));
        var objOffset = obj.position();
        var viewTop = objOffset.top + obj.outerHeight() ;
        var viewLeft = objOffset.left ;
        $(".txt-tips").css({left:viewLeft  + "px", top: viewTop + "px"}).slideDown("fast");        
      },
      hideError: function ()
      {
        $(".txt-err").fadeOut("fast");
      },
      showError: function (obj, errinfo)
      {
        $("#jx-txt-err").html(errinfo);
        var objOffset = obj.offset();
        var viewTop = objOffset.top - obj.outerHeight() ;
        var viewLeft = objOffset.left ;
        if (viewTop <=-2)
        {
          viewTop = objOffset.top ;
          viewLeft = objOffset.left + obj.outerWidth() +3
        }
        $(".txt-err").css({left:viewLeft  + "px", top: viewTop + "px"}).slideDown("fast");
      },
      showDialog: function ()
      {
        var viewinfo = '<style>.ula { list-style-type: none; margin: 0px; padding: 0px; padding-left:10px;}.lib { list-style: inside }</style><div style=" width:100%; margin:0 auto; line-height:26px; font-size:12px; max-height:400px; overflow:auto;"><div style="height:15px;"></div>';
        viewinfo += '<div style="height:auto; overflow:hidden; border-bottom:1px dashed #999; padding-left:10px; margin-bottom:5px; padding-bottom:5px;"><div style=" color:#F00; font-weight:bold; font-size:15px;"> ';
        viewinfo += $(document).attr("title");
        viewinfo += '</div><ul class="ula">';
        for (infoKey in this.checkInfos)
        {
          viewinfo += "<li class='lib'>";
          viewinfo += this.checkInfos[infoKey];
          viewinfo += "</li>";
        }
        viewinfo += '</ul></div>';
        viewinfo += "</div>"
        msgbox.alert({title:this.msgTitle,content: viewinfo},function()
        {
          
        });
      },
      setMsgboxTitle:function(title)
      {
        this.msgTitle = title ;
      },
      simpleCheck: function ()
      {
        this.checkInfos.length = 0;
        this.checkInfo = "";
        this.errorIds.length = 0;

        var flag = true;
        var cycFlag = false;

        $("textarea").each(function ()
        {
          cycFlag = $jxCheck.inputCheck($(this));
          if (!cycFlag)
            $jxCheck.checkInfos.push($jxCheck.checkInfo);

          if (flag)
            flag = cycFlag;
        });
        $("input").each(function ()
        {
          cycFlag = $jxCheck.inputCheck($(this));
          if (!cycFlag)
            $jxCheck.checkInfos.push($jxCheck.checkInfo);

          if (flag)
            flag = cycFlag;
        });

        return flag;
      },
      inputCheck: function (widget)
      {
        this.checkInfo = "";
        if (undefined == widget.attr("jtag"))
          return true;

        var jtag = "【" + widget.attr("jtag") + "】";
        var flag = true;
        if (widget.val() == "")
        {
          this.checkInfo = jtag + "不能为空";
          flag = false;
        }
        else if ((undefined != widget.attr("jsize")) && (widget.val().length > widget.attr("jsize")))
        {
          this.checkInfo = jtag + " 长度最多只能为" + widget.attr("jsize");
          flag = false;
        }
        else if ((undefined != widget.attr("jmaxsize")) && (widget.val().length > widget.attr("jmaxsize")))
        {
          this.checkInfo = jtag + " 长度最多只能为" + widget.attr("jmaxsize");
          flag = false;
        }
        else if ((undefined != widget.attr("jfixsize")) && (widget.val().length > widget.attr("jfixsize")))
        {
          this.checkInfo = jtag + " 长度只能为" + widget.attr("jfixsize");
          flag = false;
        }        
        else if ((undefined != widget.attr("jminsize")) && (widget.val().length < widget.attr("jminsize")))
        {
          this.checkInfo = jtag + " 长度最小为" + widget.attr("jminsize");
          flag = false;
        }        
        else if (undefined != widget.attr("jtype"))
        {
          if ("n" == widget.attr("jtype"))
          {
            if (/[^\d.]/g.test((widget.val())))
            {
              this.checkInfo = jtag + "只能填写数字";
              flag = false;
            }
          }
          else if ("s" == widget.attr("jtype"))
          {
            reg = /[^\\x00-\\xff]/;
            if (!reg.test(widget.val()) && (widget.val().length > widget.attr("jsize")))
            {
              this.checkInfo = jtag + "只能填写普通字符";
              flag = false;
            }
          }
          else if ('c' == widget.attr("jtype"))
          {
            reg = /^[\u0391-\uFFE5]+$/;
            if (!reg.test(widget.val()))
            {
              this.checkInfo = jtag + "只能输入汉字";
              flag = false;
            }
          }
          else if ('m' == widget.attr("jtype")) {
            if (!(jxKit.checkUNDecimal(widget.val()))) {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            } else {
              var valuesnum = Number(widget.val());
              widget.val(valuesnum.toFixed(2));
            }

          } else if ('i' == widget.attr("jtype")) {
            if (!(jxKit.checkUNInt(widget.val()))) {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            } else {
              widget.val(parseInt(widget.val()));
            }
          }          
          else if ('phone' == widget.attr("jtype"))
          {
            if (!(jxKit.checkPhone(widget.val())))
            {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            }
          }
          else if ('mobile' == widget.attr("jtype"))
          {
            if (!(jxKit.checkMobile(widget.val())))
            {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            }
          }
          else if ('card' == widget.attr("jtype"))
          {
            if (!(jxKit.checkIdCard(widget.val())))
            {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            }
          }
          else if ('email' == widget.attr("jtype"))
          {
            if (!(jxKit.checkEmail(widget.val())))
            {
              this.checkInfo = jtag + "格式有误";
              flag = false;
            }
          }
        }
        if (!flag)
          this.addErrorId(widget.attr("name"));
        return flag;
      }
    };
  $(function ()
  {
    $(document).on("focus", "textarea,:text,select", function ()
    {
      $jxCheck.inputFocus($(this));
    });
    $(document).on("blur", "textarea,:text,select", function ()
    {
      $jxCheck.inputBlur($(this));
    });

    $("textarea,:text,select").on("change", function ()
    {
      if (undefined == $(this).attr("jxcheck"))
        $jxCheck.updateFlag = false;
    })
  });
  window.jxCheck = $jxCheck;
});